package site.addzero.repl

import kotlinx.coroutines.runBlocking
import org.koin.core.annotation.Single
import site.addzero.app.AdvancedRepl
import site.addzero.app.ParamDef
import site.addzero.cli.config.Lines
import site.addzero.cli.config.configService
import site.addzero.cli.dotfiles.sync_stragty.SyncUtil
import site.addzero.cli.setting.SettingContext.HOME_DIR
import site.addzero.cli.setting.SettingContext.absParamDescription
import site.addzero.util.AddFileUtil.undoMvln
import site.addzero.util.KoinInjector.inject
import site.addzero.util.SourcePathResolver
import site.addzero.util.StatusPrinter
import kotlin.reflect.typeOf


/**
 * AddCommand的REPL包装类，实现AdvancedRepl接口
 */
@Single
class RmDotFilesRepl : AdvancedRepl<RmDotFilesRepl.AddCommandParams, Unit> {
    override val command: String = "rm-dotfiles"
    override val description: String = "移除dotfiles软连接"
    override val paramDefs: List<ParamDef> = listOf(
        ParamDef(
            name = "sourcePath",
            type = typeOf<String>(),
            description = "要删除的文件路径(从已有链接里复制传参即可,支持批量传参)"
        ),
        ParamDef(
            name = "abs",
            type = typeOf<Boolean>(),
            description = absParamDescription,
            defaultValue = false
        ),
    )

    data class AddCommandParams(
        val sourcePath: String,
        val abs: Boolean = false,
    )

    override fun eval(params: AddCommandParams) = runBlocking {
        //todo: 删除软连接
        val abs = params.abs
        val sourcePaths = params.sourcePath.split(",").map { it.trim() }.filter { it.isNotEmpty() }
        val currentPlatformConfig = configService.currentPlatformConfig

        val sourcePathResolver = inject<SourcePathResolver>()

        for (sourcePath in sourcePaths) {
            val newSourceDirAbs = sourcePathResolver.resolveSourcePath(sourcePath, abs)

            //删掉links配置
            val links = currentPlatformConfig.links
            val filteredLinks = links.filterNot { it.source == newSourceDirAbs }

            // 如果过滤后的链接数量与原来不同，说明找到了要删除的链接
            if (filteredLinks.size != links.size) {
                val copy = currentPlatformConfig.copy(links = filteredLinks.toSet())
                configService.updatePlatFormConfig(copy)
                println("已从配置中移除链接: $newSourceDirAbs")
            } else {
                println("未在配置中找到链接: $newSourceDirAbs")
            }

            if (newSourceDirAbs == null) {
                System.err.println("dotfiles符号链接不存在")
                // 上传配置
                SyncUtil.commitAndPush()
                continue
            }

            undoMvln(newSourceDirAbs)
            // 上传配置
            SyncUtil.commitAndPush()
            println("源文件已不再链接")
        }

        // 打印当前的软连接和同步软件包信息
        val statusPrinter = inject<StatusPrinter>()
        statusPrinter.printCurrentStatus()
    }

//    override fun createParams(values: List<Any?>): AddCommandParams {
//        return AddCommandParams(
//            sourcePath = values[0] as String,
//            abs = values.getOrNull(1) as? Boolean ?: false,
//        )
//    }
}
